package com.meitianhui.convenitenbuy.module.home.presenter;

import com.google.gson.Gson;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.module.home.contract.HomeSearchContract;
import com.meitianhui.convenitenbuy.module.home.http.HomeApiDataSource;
import com.meitianhui.convenitenbuy.module.home.model.bean.HomeGoodsInfo;
import com.meitianhui.convenitenbuy.module.home.ui.activity.HomeSearchActivity;

import java.util.HashMap;
import java.util.Map;

public class HomeSearchPresenter implements HomeSearchContract.Presenter {

    private HomeSearchActivity context;
    private HomeSearchContract.View view;

    public HomeSearchPresenter(HomeSearchContract.View view, HomeSearchActivity context) {
        this.view = view;
        this.context = context;
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

    @Override
    public void searchGoods(String pageIndex, String pageSize, String search) {
        Map<String, String> maps = new HashMap<>();
        maps.put("page_no", pageIndex + "");
        maps.put("page_size", pageSize + "");
        String pageJson = new Gson().toJson(maps);

        Map<String, String> map = new HashMap<>();
        map.put("search", search);
        String paramJson = new Gson().toJson(map);

        HomeApiDataSource.getInstance()
                .getEdinburgh(InfoProvider.getAppToken(App.getInstance()),
                        "psGoods.consumer.freeGetGoodsSearchListPageFind", pageJson, paramJson)
                .subscribe(new ResponseObserver<BaseResponse<HomeGoodsInfo>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<HomeGoodsInfo> response) {
                        view.searchGoodsSuccess(response.data);
                    }
                });
    }

}
